package com.yao.club.service.impl;


import com.yao.club.entity.dto.MallActivity;
import com.yao.club.entity.dto.MallAd;
import com.yao.club.entity.dto.MallGoodsInfo;
import com.yao.club.entity.dto.MallGroup;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.entity.vo.MallGroupVO;
import com.yao.club.reposittory.MallActivityRepository;
import com.yao.club.reposittory.MallAdRepository;
import com.yao.club.reposittory.MallGoodsInfoRepository;
import com.yao.club.reposittory.MallGroupRepository;
import com.yao.club.service.MallHomeService;
import com.yao.club.utils.ResponseUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MallHomeServiceImpl implements MallHomeService {
    @Autowired
    MallAdRepository mallAdRepository;
    @Autowired
    MallActivityRepository mallActivityRepository;
    @Autowired
    MallGroupRepository mallGroupRepository;
    @Autowired
    MallGoodsInfoRepository mallGoodsInfoRepository;
    /**
     * 获得商品首页广告信息
     * @return
     */
    @Override
    public ResponseEntity<MallAd> cmallAdInfo() {
        List<MallAd> all = mallAdRepository.findAll();
        if (all != null && all.size() > 0) {
            return ResponseUtils.success(all.get(0));
        }
        return ResponseUtils.failed("失败");
    }
    /**
     * 获得商品首页轮播图
     * @return
     */
    @Override
    public ResponseEntity<List<MallAd>> getHomeBannerInfo() {
        List<MallAd> all = mallAdRepository.findAll();
        if (all != null && all.size() > 0) {
            return ResponseUtils.success(all);
        }
        return ResponseUtils.failed("失败");
    }
    /**
     * 获得商品首活动
     * @return
     */
    @Override
    public ResponseEntity<List<MallActivity>> getHomeActivityInfo() {
        List<MallActivity> all = mallActivityRepository.findAll();
        if (all != null && all.size() > 0) {
            return ResponseUtils.success(all);
        }
        return ResponseUtils.failed("失败");
    }

    @Override
    public ResponseEntity<MallAd> cmallFloatAdInfo() {
        List<MallAd> all = mallAdRepository.findAll();
        if (all != null && all.size() > 1) {
            return ResponseUtils.success(all.get(1));
        }
        return ResponseUtils.failed("失败");
    }


    @Override
    public ResponseEntity<List<MallGroupVO>> cMallGroupList() {
        List<MallGroup> cHome = mallGroupRepository.findMallGroupByType("c_home");
        List<MallGroupVO> cHomeVOList = new ArrayList<>();
        if (cHome!= null && cHome.size() > 0) {
            for (MallGroup mallGroup : cHome) {
                MallGroupVO mallGroupVO = new MallGroupVO();
                BeanUtils.copyProperties(mallGroup, mallGroupVO);
                //查询分组数据
                List<MallGoodsInfo> mallGoodsInfosByGroupId = mallGoodsInfoRepository.findMallGoodsInfosByGroupId(mallGroup.getId());
                mallGroupVO.setMallGoodsInfoPageResponseList(mallGoodsInfosByGroupId);
                cHomeVOList.add(mallGroupVO);
            }

        }
        return ResponseUtils.success(cHomeVOList);
    }

    @Override
    public ResponseEntity<List<MallGroup>> mallGroupIndexList() {
        List<MallGroup> all = mallGroupRepository.findAll();
        if (all != null && all.size() > 0) {
            return ResponseUtils.success(all);
        }
        return ResponseUtils.failed("失败");
    }
}
